<?php

/* @author      :   THUYNT
 * @name        :   CateController
 * @copyright   :   Fpt Online
 * @todo        :   Cate Controller
 */
class Euro2012_CateController extends Zend_Controller_Action
{

    /**
     * init of controller
     */
    public function init()
    {
       $category = Category::getInstance();
        if($this->_request->getParam('catecode'))
        {
            $catename = $this->_request->getParam('catecode');
        }
        else
        {
            $catename = explode('/', $this->_request->getParam('catename'));
            if(count($catename)<=2)
            {
                if(count($catename)==2)
                {
                    $cateInfo = $category->getCategoryByCode($catename[0]);
                    $this->_request->setParam('pcateid', $cateInfo['category_id']);
                }
                $catename = end($catename);
            }
            else
            {
                $pageIdx = array_search('page', $catename);
                $page = (isset($catename[$pageIdx+1]))?(int)$catename[$pageIdx+1]:1;
                $catename = (isset($catename[$pageIdx-1]))?$catename[$pageIdx-1]:'';
                $this->_request->setParam('page', $page);
            }
        }
        $cateInfo = $category->getCategoryByCode($catename);
        if($cateInfo)
        {
            $this->_request->setParam('cateid', $cateInfo['category_id']);
        }
        //Assign metadata
        $this->view->headTitle()->prepend(META_TITLE);
        $this->view->headMeta()->prependName('keywords',META_KEYWORDS);
        $this->view->headMeta()->prependName('description',META_DESC);
    }

    //index action
    public function indexAction()
    {        
        $intCateID = $this->_request->getParam('cateid');        
        $limit = 4;
        //Get instance
        $instanceDB = News::getInstance();
        //default articleType
        $articleType = NULL;//ARTICLE;

        if (empty($intCateID)) {
            $intCateID = SITE_ID;
            //homepage: get all type
            $articleType = NULL;
        }

        //Result hot news
        if($intCateID == SITE_ID)
        {
            /* Get box hot news */
            $arrParams = array(
                'category_id' => $intCateID,
                'article_type' => $articleType,
                'limit' => $limit,
                'offset' => 0,
                'site_id' => SITE_ID
            );

            $arrArticleID = $instanceDB->getListArticleByHot($arrParams);
        }
        else
        {
            $arrArticleID = $instanceDB->getListTopArticle($intCateID, $limit, 0, $articleType);
        }


        $arrHotNewsResult = $instanceDB->getDetailArticleByIDs($arrArticleID['data']);

        //get new by category       
        //Get parent Params
        $intLimit = LIMIT_NEW_MORE;
        //$parentParams = Fpt_Global::getParentParams();
        $intPage = $this->_request->getParam('page',1);
        $offset = ($intPage - 1) * $intLimit;

        // Get news instance
        $newsModel = News::getInstance();
        //get list articleid
        $arrArticle = $newsModel->getListTopArticle($intCateID, $intLimit, $offset, NULL);

        //get detail Article by list id
        if (!empty($arrArticle) && $arrArticle["total"] > 0) {
            $arrArticle["data"] = $newsModel->getDetailArticleByIDs($arrArticle["data"]);
        }

        
        $currentCateCode = $this->view->arrMenu[$intCateID]['catecode'];      
        
        //Set strUri
        $strUri = '/'.$currentCateCode.'/page';

        //InitParam Paging
        $arrParamPaging = array(
            'total'         =>  $arrArticle["total"],
            'page'          =>  $intPage,
            'uri'           =>  $strUri,
            'showItem'      =>  3,
            'perpage'       =>  $intLimit,
            'idPagination'  =>  'pagination',
            'extEnd'        =>  '.html',
            'separate'      =>  '/'
        );
        
        //Assign paging to view
        $this->view->arrParamPaging = $arrParamPaging;       

        //Assign to view
        $this->view->arrData = $arrArticle["data"];
        $this->view->articleCategoryID = trim($catID);
        $this->view->limitPage = $intLimit;
        $this->view->arrHotNewsResult = $arrHotNewsResult;

    }    
}
 ?>